package com.group9.controller;

import com.group9.params.ElasticInfoQuery;
import com.group9.pojo.ElasticFruitInfo;
import com.group9.pojo.FruitInfo;
import com.group9.service.ElasticFruitInfoService;
import com.group9.service.FruitInfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QZP on 2020/5/27.
 */

//@Controller
public class ElasticFruitInfoController {

    @Autowired
    ElasticFruitInfoService elasticFruitInfoService;

    @Autowired
    FruitInfosService fruitInfosService;



    @RequestMapping("saveallelasricinfo")
    public List<ElasticFruitInfo> saveElasticInfo() {
        List<FruitInfo> fruitInfos = fruitInfosService.selectAll();
        fruitInfos.forEach(System.out::println);
        List<ElasticFruitInfo> elasticFruitInfoList = new ArrayList<>();
        elasticFruitInfoList.add((ElasticFruitInfo) fruitInfos);
        elasticFruitInfoService.saveAllElasticInfo(elasticFruitInfoList);
        return elasticFruitInfoList;

    }

    @RequestMapping("getAllElasticInfo")
    @ResponseBody
    public Iterable<ElasticFruitInfo> getAll(){
        return  elasticFruitInfoService.getAll();
    }

    @RequestMapping("findbypricebetween")
    @ResponseBody
    public List<ElasticFruitInfo> findByFruitPriceBetween(double low,double up){
        return elasticFruitInfoService.findByFruitPriceBetween(low,up);
    }


    @RequestMapping("iksearchinfo")
    @ResponseBody
    public Page<ElasticFruitInfo> ikSearch(String field,String queryString){
        return elasticFruitInfoService.ikSearch(field,queryString);
    }

    @RequestMapping("iksearchpageinfo")
    @ResponseBody
    public Page<ElasticFruitInfo> ikSearch(ElasticInfoQuery elasticInfoQuery){
        return elasticFruitInfoService.ikSearchByPage(elasticInfoQuery);
    }

}
